#import "SMUserMomentsViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "SMMomentCell.h"
#import "SMMoment.h"
#import "SMComment.h"

@interface SMUserMomentsViewController ()<SMMomentCellDelegate>
@property(nonatomic, copy) NSString *uid;
@property(nonatomic, strong) NSMutableArray<SMMoment*> *data;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, assign) BOOL hasMore;
@end

@implementation SMUserMomentsViewController
- (instancetype)initWithUserId:(NSString *)uid { if (self=[super initWithStyle:UITableViewStylePlain]) { _uid=[uid copy]; } return self; }
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor=UIColor.systemGroupedBackgroundColor;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:SMMomentCell.class forCellReuseIdentifier:@"moment"];
    self.data=[NSMutableArray array]; self.page=1; self.hasMore=YES;
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    [self.tableView.mj_header beginRefreshing];
}
- (void)refresh { self.page=1; self.hasMore=YES; [self requestPage:self.page]; }
- (void)loadMore { if(self.hasMore){ self.page++; [self requestPage:self.page]; } else [self.tableView.mj_footer endRefreshingWithNoMoreData]; }
- (void)requestPage:(NSInteger)page {
    NSMutableArray *arr=[NSMutableArray array];
    for (int i=0;i<10;i++){
        SMMoment *m=[SMMoment new];
        m.momentId=[NSString stringWithFormat:@"%@-%ld-%d", self.uid, (long)page, i];
        m.author=@"Ta"; m.avatarURL=[NSString stringWithFormat:@"https://i.pravatar.cc/100?u=%@", self.uid?:@"u"];
        m.text=@"示例动态：九宫格、点赞、评论、三行展开，保持小清新风格。";
        int c=arc4random_uniform(9)+1; NSMutableArray *imgs=[NSMutableArray array];
        for(int k=0;k<c;k++) [imgs addObject:[NSString stringWithFormat:@"https://picsum.photos/240/240?seed=%@_%d", self.uid, k]];
        m.imageURLs=imgs; m.createdAt=[NSDate dateWithTimeIntervalSinceNow:-(arc4random_uniform(5*24*3600))]; m.likesCount=arc4random_uniform(300); m.isLiked=NO; m.isExpanded=NO;
        [arr addObject:m];
    }
    [self applyData:arr page:page];
}
- (void)applyData:(NSArray<SMMoment*>*)arr page:(NSInteger)page {
    if (page==1) [self.data removeAllObjects];
    [self.data addObjectsFromArray:arr];
    self.hasMore = arr.count>=10;
    [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
    if (self.hasMore) [self.tableView.mj_footer endRefreshing]; else [self.tableView.mj_footer endRefreshingWithNoMoreData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.data.count; }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [SMMomentCell heightForMoment:self.data[indexPath.row] width:UIScreen.mainScreen.bounds.size.width]; }
- (UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)ip { SMMomentCell *c=[tv dequeueReusableCellWithIdentifier:@"moment" forIndexPath:ip]; [c configure:self.data[ip.row]]; c.delegate=self; return c; }
- (void)momentCellDidTapLike:(NSString *)momentId { for (SMMoment *m in self.data) if ([m.momentId isEqualToString:momentId]) { m.isLiked=!m.isLiked; m.likesCount+=m.isLiked?1:-1; break; } [self.tableView reloadData]; }
- (void)momentCellDidTapComment:(NSString *)momentId {}
- (void)momentCellDidToggleExpand:(NSString *)momentId {
    NSInteger row = [self.data indexOfObjectPassingTest:^BOOL(SMMoment * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { return [obj.momentId isEqualToString:momentId]; }];
    if (row==NSNotFound) return; SMMoment *m=self.data[row]; m.isExpanded=!m.isExpanded;
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
